package com.ming.controller;

import com.ming.dao.CustomerMapper;
import com.ming.dao.RecipientMapper;
import com.ming.pojo.Customer;
import com.ming.pojo.Recipient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Xu
 * Created by Intellij IDEA.
 * @create 2021/4/15 10:07
 */
@Controller
public class RecipientController {
    @Autowired
    RecipientMapper recipientMapper;

    @RequestMapping("/recipients")
    public String list(Model model){
        List<Recipient> recipients = recipientMapper.queryRecipients();
        model.addAttribute("recipients",recipients);
        return "recipients/list";
    }

    @GetMapping("/recipient")
    public String toAddPage(Model model){
        //查出所有部门信息
        List<Recipient> recipients = recipientMapper.queryRecipients();
        model.addAttribute("recipients",recipients);
        return "recipients/add";
    }

    @PostMapping("/recipient")
    public String addRecipient(Recipient recipient){
        recipient.setRid(recipientMapper.getMId());
        recipientMapper.addRecipient(recipient);
        return "redirect:/recipients";
    }

    //去客户的修改页面
    @GetMapping("/recipient/{id}")
    public String toUpdateRecipient(@PathVariable("id") int id, Model model) {
        //查出原来的数据
        Recipient recipient = recipientMapper.queryRecipientById(id);

        model.addAttribute("recipient", recipient);
        return "recipients/update";
    }

    @PostMapping("/updateRecipient")
    public String updateCustomer(Recipient recipient) {
        System.out.println(recipient.getAddress());
        recipientMapper.updateRecipient(recipient);
        return "redirect:/recipients";
    }


    @GetMapping("/deleteRecipient/{id}")
    public String deleteRecipient(@PathVariable("id")Integer id){
        recipientMapper.deleteRecipient(id);
        return "redirect:/recipients";
    }

}
